package com.booza.dao;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.booza.model.Tournament;
import com.booza.model.TournamentDef;
import com.booza.model.TournamentInstance;
import com.booza.model.TournamentPlayers;

/**
 * The DAO level interface for tournament management.
 */
public interface TournamentDAO extends CommonDAO
{
	/**
	 * Inserts tournament details in to tournament table.
	 * 
	 * @param tr
	 *          -row to be inserted
	 */
	void createTournament(Tournament tr);

	/**
	 * Find tournament by tournament ID.
	 * 
	 * @param id
	 *          - primary key of the table
	 * @return Return the matching tournament.
	 */
	Tournament findTournamentById(Integer id);

	/**
	 * Obtains all tournaments so far.
	 * 
	 * @return Return a list of all tournaments.
	 */
	Collection<Tournament> fetchAll();

	/**
	 * Find tournaments matching the criteria.
	 * 
	 * @param colName
	 *          - colname of the criteria
	 * @param colValue
	 *          - keyword
	 * @return Return matching tournaments.
	 */
	Collection<Tournament> fetchAllByParam(String colName, Object colValue);

	/**
	 * Adds player to the tournament.
	 * 
	 * @param tp
	 *          -player to be added
	 */
	void addPlayerToTournament(TournamentPlayers tp);

	/**
	 * Find players for the given tournament.
	 * 
	 * @param id
	 *          - id of the tournament
	 * @return list of players participating in the tournament
	 */
	Collection<TournamentPlayers> findPlayersForTournament(Integer id);

	/**
	 * Executes a generic query in tournament table.
	 * 
	 * @param query
	 *          -query to be executed
	 * @param colName
	 *          - column in the criteria
	 * @param val
	 *          - value to be applied
	 * @return Return results matching the criteria.
	 */
	Collection<Tournament> executeQuery(String query, String colName, Object val);

	/**
	 * Executes a generic query in <code>tournamentplayers</code> table.
	 * 
	 * @param query
	 *          -query to be executed
	 * @param colName
	 *          - column in the criteria
	 * @param val
	 *          - value to be applied
	 * @return Return results matching the criteria.
	 */
	Collection<TournamentPlayers> executeQueryForPlayers(String query, String colName, Object val);

	/**
	 * Checks if the player has been added already for the given tournament.
	 * 
	 * @param accountId
	 *          - the player ID
	 * @param tournamentId
	 *          - the tournament ID
	 * @return Return player details if already added, null if not.
	 */
	TournamentPlayers findPlayersById(Integer accountId, Integer tournamentId);

	/**
	 * Find list of players invited for the tournament.
	 * 
	 * @param friendId
	 * @param tournamentId
	 *          - ID of tournament
	 * @return list of players
	 */
	Collection<TournamentPlayers> getInvitedPlayers(Integer friendId, Integer tournamentId);

	/**
	 * Update the tournament player details.
	 * 
	 * @param player
	 *          - details to be updated
	 */
	void updateTournamentPlayer(TournamentPlayers player);

	/**
	 * @param accountId
	 * @param startTime
	 * @return
	 */
	Collection<TournamentPlayers> getAccepted(Integer accountId, Date startTime);

	Collection<TournamentPlayers> executeQuery(String query, Object obj);

	/**
	 * Find players who has accepted a tournament invitation which starts at the given time.
	 * 
	 * @param startTime
	 *          - time at which the tournament starts
	 * @param accountId
	 *          -
	 * @return players
	 */
	Collection<TournamentPlayers> getConflictingTournaments(Date startTime, Integer accountId);

	/**
	 * Find list of system generated tournaments.
	 * 
	 * @return list of system generated tournaments
	 */
	List<TournamentDef> getSystemTournaments();

	/**
	 * Find list of tournaments which starts tomorrow, for which players have accepted.
	 * 
	 * @return list of system generated tournaments
	 */
	List getTournamentsEligibleForReminder();

	/**
	 * Find tournaments to be held in future time. Finds tournaments created by the user.
	 * 
	 * @param accountId
	 *          - id of the user
	 * @return matching tournament details
	 */
	Collection<Tournament> findUpcomingTournaments(Integer accountId);

	/**
	 * Find tournaments created by the user and whose status is finished.
	 * 
	 * @param accountId
	 *          - id of the user
	 * @return matching tournament details
	 */
	Collection<Tournament> findPreviousTournaments(Integer accountId);

	/**
	 * Find tournaments for which invitations can be sent.
	 * 
	 * @param accountId
	 *          - id of the user
	 * @return matching tournament details
	 */
	Collection<Tournament> findUpcomingTournamentsCanInvite(Integer accountId);

	/**
	 * Find tournaments for which invitations has been sent
	 * 
	 * @param accountId
	 *          - id of the user
	 * @return matching tournament details
	 */
	Collection<Tournament> findTournamentsInvitations(Integer accountId);

	/**
	 * Find the winner of a specific tournament.
	 * 
	 * @param tournamentid
	 * @return
	 */
	String findWinnerOfTournament(Integer tournamentId, String tournamentType);

	/**
	 * Find details about the given tournament. Retrieves and winner/loser information and their respective scores of all rounds.
	 * 
	 * @param tournamentId
	 * @return
	 */
	List getTournamentDetails(Integer tournamentId, String tournamentType);

	/**
	 * Find tournament by tournament name.
	 * 
	 * @param name
	 * @return
	 */
	Tournament findTournamentByName(String name);

	/**
	 * Finds winner names for all tournaments of this type.
	 * 
	 * @param tournamentId
	 * @param tournamentType
	 * @return
	 */
	List findWinnerOfAllTournaments(List<Integer> tournamentId, String tournamentType);

	/**
	 * Find a system tournament by primary key.
	 * 
	 * @param id
	 * @return
	 */
	TournamentDef findSystemTournament(Integer id);

	/**
	 * Find a running tournament involving this user.
	 * 
	 * @param accountId
	 * @return
	 */
	Collection<Tournament> findRunningTournaments(Integer accountId);

	/**
	 * Find system tournaments which are yet to commence.
	 */
	Collection<TournamentDef> findFreshSysTournaments();

	boolean deleteTournament(Tournament tournament);

	Collection<Tournament> findDraftTournaments(Integer accountId);

	boolean changeTournamentStatus(Integer accountid, Integer tornamentId, int status);

	void updateTournament(Integer accountid, Tournament tournament);

	List<TournamentInstance> getSystemGeneratedTournamentIntances();
	
	/**
	 * Updates reminded column as 'reminded(1)' for the list of playerrefids supplied
	 * @param listofids - comma separated playerrefid s
	 */
	int markRemindedEmailsAsSent(String listofIds);
}
